import {
  AsyncStorage,
} from 'react-native';
import Config from './config';
import util from './util';


// const getRequestOption = async (method = "GET") => {
//   // let tokenData = await AsyncStorage.getItem(Config.token_key);
//   let tokenData = null;
//   console.log("getRequestOption, tokenData: ", tokenData);
//   let token = defaultToken;
//   if (tokenData) {
//     tokenData = JSON.parse(tokenData);
//     token = tokenData.access_token;
//   }
//   return {
//     method: method,
//     headers: {
//       "Accept": "application/json",
//       "Content-Type": "application/json",
//       "Authorization": "Bearer " + token,
//     },
//   }
// }

const get = async (uri, queryString = "", noBaseUri = false) => {
  let options = await util.getRequestOption('GET');
  let url = Config.base_uri + uri;
  if (noBaseUri) {
    url = uri;
  }
  if (queryString) {
    url += "?" + queryString;
  }
  console.log("doing get rquest...., options: ", options, ", url: ", url);
  return fetch(url, options).then(response => response.json());
}

const post = async (uri, dataObject = null) => {
  let options = await util.getRequestOption('POST');
  let url = Config.base_uri + uri;
  if (dataObject) {
    options.body = JSON.stringify(dataObject);
  }
  console.log("doing post rquest...., options: ", options, ", url: ", url);
  return fetch(url, options).then(response => response.json());
}

const api = {
  login: async (dataObject = null) => {
    return await post("forumtaxonomy", dataObject);
  },
  listForumtaxonomy: async (params = "") => {
    return await get("forumtaxonomy", params);
  },
  listForum: async (params = "") => {
    return await get("forum", params);
  },
  listTopic: async (params = "") => {
    return await get("topic", params);
  },
  createTopic: async (params = "") => {
    return await post("topic", params);
  },
  listComment: async (params = "") => {
    return await get("comment", params);
  },
  createComment: async (params = "") => {
    return await post("comment", params);
  },
  listCommentComment: async (params = "") => {
    return await get("commentcomment", params);
  },
  uploadToken: async (params = "") => {
    return await get("upload/token", params);
  }
  
  
}

module.exports = api;